﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class ConfigLoader : MonoBehaviour
{
    public int UDPPort { get; private set; } = 33200;

    [SerializeField]
    RangeDevice RangeDeviceTemplate;
    [SerializeField]
    ToggleDevice ToggleDeviceTemplate;

    const string ConfigFileName = "Config.json";
    const string ConfigExampleFileName = "Config.example.json";
    // Start is called before the first frame update
    void Start()
    {
        var path = Path.Combine(Application.streamingAssetsPath, ConfigFileName);
        if (!File.Exists(path))
            CreateDefault();

        var jsonStr = File.ReadAllText(path, Encoding.UTF8);
        try
        {
            var config = JsonConvert.DeserializeObject<Config>(jsonStr);
            UDPPort = config.UDPPort;
            foreach (var rangeDev in config.RangeDevices)
            {
                var go = Instantiate<GameObject>(RangeDeviceTemplate.gameObject);
                go.name = rangeDev.Name;
                var ctrl = go.GetComponent<RangeDevice>();
                ctrl.Name = rangeDev.Name;
                ctrl.SetDevType(rangeDev.Type);
                ctrl.SetDevId(rangeDev.Id);
                ctrl.SetRange(rangeDev.Range);
            }

            foreach (var toggleDev in config.ToggleDevices)
            {
                var go = Instantiate<GameObject>(ToggleDeviceTemplate.gameObject);
                go.name = toggleDev.Name;
                var ctrl = go.GetComponent<ToggleDevice>();
                ctrl.Name = toggleDev.Name;
                ctrl.SetDevType(toggleDev.Type);
                ctrl.SetDevId(toggleDev.Id);
            }
        }
        catch (Exception e)
        {
            Debug.LogException(e);
        }
    }

    void CreateDefault()
    {
        var path = Path.Combine(Application.streamingAssetsPath, ConfigFileName);
        var examplePath = Path.Combine(Application.streamingAssetsPath, ConfigExampleFileName);
        if (File.Exists(path))
            return;

        var cfg = new Config();

        var door0 = new RangeDeviceConfig();
        door0.Name = "门1";
        door0.Type = "door";
        door0.Id = "0";
        door0.Range = -90;
        cfg.RangeDevices.Add(door0);

        for (int i = 0; i < 11; i++)
        {
            var valve = new RangeDeviceConfig();
            valve.Name = "阀门" + (i + 1).ToString();
            valve.Type = "encoder";
            valve.Id = i.ToString();
            valve.Range = 720;
            cfg.RangeDevices.Add(valve);
        }


        var btn0 = new ToggleDeviceConfig();
        btn0.Name = "按钮1";
        btn0.Type = "key";
        btn0.Id = "100";
        cfg.ToggleDevices.Add(btn0);

        var btn1 = new ToggleDeviceConfig();
        btn1.Name = "按钮2";
        btn1.Type = "key";
        btn1.Id = "0";
        cfg.ToggleDevices.Add(btn1);

        var btn2 = new ToggleDeviceConfig();
        btn2.Name = "按钮3";
        btn2.Type = "key";
        btn2.Id = "1";
        cfg.ToggleDevices.Add(btn2);

        var fan0 = new ToggleDeviceConfig();
        fan0.Name = "风感1";
        fan0.Type = "fan";
        fan0.Id = "0";
        cfg.ToggleDevices.Add(fan0);

        var torch0 = new ToggleDeviceConfig();
        torch0.Name = "手电筒1";
        torch0.Type = "flashlight";
        torch0.Id = "0";
        cfg.ToggleDevices.Add(torch0);

        var settings = new JsonSerializerSettings();
        settings.Formatting = Formatting.Indented;
        var jsonStr = JsonConvert.SerializeObject(cfg, settings);
        File.WriteAllText(examplePath, jsonStr, Encoding.UTF8);
        File.WriteAllText(path, jsonStr, Encoding.UTF8);
    }
}
